/*
 Copyright (C) 2010 Kristian Duske

 This file is part of TrenchBroom.

 TrenchBroom is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 TrenchBroom is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "io/ImageLoader.h"

#include <FreeImage.h>
#include <filesystem>
#include <vector>

namespace tb::io
{

class InitFreeImage
{
private:
  InitFreeImage();
  ~InitFreeImage();

public:
  static void initialize();
};

class ImageLoaderImpl
{
private:
  FIMEMORY* m_stream = nullptr;
  FIBITMAP* m_bitmap = nullptr;

public:
  ImageLoaderImpl(ImageLoader::Format format, const std::filesystem::path& path);
  ImageLoaderImpl(ImageLoader::Format format, const char* begin, const char* end);
  ~ImageLoaderImpl();

  size_t paletteSize() const;
  size_t bitsPerPixel() const;
  size_t width() const;
  size_t height() const;
  size_t byteWidth() const;
  size_t scanWidth() const;

  bool hasPalette() const;
  bool hasIndices() const;
  bool hasPixels() const;

  std::vector<unsigned char> loadPalette() const;
  std::vector<unsigned char> loadIndices() const;
  std::vector<unsigned char> loadPixels(ImageLoader::PixelFormat format) const;

private:
  std::vector<unsigned char> loadIndexedPixels(size_t pSize) const;
  std::vector<unsigned char> loadPixels(size_t pSize) const;
  static FREE_IMAGE_FORMAT translateFormat(ImageLoader::Format format);
  static size_t pixelSize(ImageLoader::PixelFormat format);
};

} // namespace tb::io
